﻿using GameCore;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIState_SHLoadingState : EntityState
{

    public static bool  doNothing = false;

    public   UIState_SHLoadingState(int iStateId, EntityState eState) : base(iStateId, eState) { }

    protected override void OnStateBegin(object[] command)
    {
        if (doNothing)
        {
            doNothing = false;
            return;
        }
        GameObject gameManager = GameObject.Find("StaticSource");
        if (gameManager != null)
        {
            //从登录过度到大厅界面
            if (gameManager.GetComponent<MainLogic>().uiStateManager.GetOldStateID() == (int)GameCore.UIState.UIState_SHLoginState )
            {
                UIManager.ChangeUI(UIManager.PageState.LoadingPage, (GameObject obj) =>
                {
                    GameObject loginPage = GameObject.Find("LoginPage(Clone)");
                    if (loginPage)
                    {
                        Destroy(loginPage);
                    }
                    obj.GetComponent<LoadingScenePage>().callBack += delegate () { GoToSHGameHall(); };          //这里是callBack,所以是加载到新的场景才切换的状态
                    obj.GetComponent<LoadingScenePage>().sceneName = "SHGameHall";                                       //这里的sceneName指的是异步加载的场景
                    obj.GetComponent<LoadingScenePage>().InformationSetting();
                });
            }

            //从大厅过度到登录界面
            else if (gameManager.GetComponent<MainLogic>().uiStateManager.GetOldStateID() == (int)GameCore.UIState.UIState_SHGameHallState)
            {
                UIManager.ChangeUI(UIManager.PageState.LoadingPage, (GameObject obj) =>
                {
                    GameObject GameHall = GameObject.Find("SHGameHall(Clone)");
                    if (GameHall)
                    {
                        Destroy(GameHall);
                    }
                    obj.GetComponent<LoadingScenePage>().callBack += delegate () { GoToSHLogin(); };
                    obj.GetComponent<LoadingScenePage>().sceneName = "LoginReq";
                    obj.GetComponent<LoadingScenePage>().InformationSetting();
                });
            }
        }
    }

    public void GoToSHGameHall()
    {
        GameObject.Find("StaticSource").GetComponent<MainLogic>().ChangeState((int)GameCore.UIState.UIState_SHGameHallState);
        GameObject.Find("StaticSource").GetComponent<MainLogic>().SetBGMusic();
    }

    public void GoToSHLogin()
    {
        GameObject.Find("StaticSource").GetComponent<MainLogic>().ChangeState((int)GameCore.UIState.UIState_SHLoginState);
        GameObject.Find("StaticSource").GetComponent<MainLogic>().SetBGMusic();
    }


    //-------------------------------------------------------------------------
    protected override void OnStateEnd()
    {
    }

    //-------------------------------------------------------------------------
    protected override void OnUpdate()
    {

    }

    public override void OnFinishedMsg(string id)
    {

    }
}
